#### Batch Find-and-Replace Script (Python)

##### Overview

This Python script performs batch find-and-replace operations across files of any type — including .html, .xml, .js, .json, .py, .css, and .scss. It scans a target directory recursively, replaces specified text, and rewrites only the modified files.

---

##### Python Installation

If Python isn’t installed yet:

###### On Windows
1. Download from python.org
2. Run the installer and check “Add Python to PATH”
3. Verify with:
   ```bash
   python --version
   ```

###### On macOS/Linux
Python is usually pre-installed. To check:
```bash
python3 --version
```
If missing, install via:
```bash
brew install python3       # macOS (Homebrew)
sudo apt install python3   # Ubuntu/Debian
```

---

#### Usage

Run the script:
```bash
python findreplace.py <directory> <oldword> <new_word> <extensions>
```

Example:
```bash
python find_replace.py ./src/ "foo" "bar" .js,.json,.py
```

**Parameters:**
- directory: Folder to scan
- old_word: Text to find
- new_word: Replacement text
- extensions: Comma-separated list of file types (e.g., .html,.xml)

---

**Features**
- Works with multiple file types
- Handles UTF-8 and UTF-16 encodings
- Skips unreadable files with warnings
- Only rewrites files if changes are made

---

#### Full Code: find_replace.py

```python
import os
import sys

def replace_in_file(filepath, oldword, newword):
    try:
        with open(filepath, 'r', encoding='utf-8') as f:
            content = f.read()
    except UnicodeDecodeError:
        try:
            with open(filepath, 'r', encoding='utf-16') as f:
                content = f.read()
        except Exception:
            print(f"⚠️ Skipped (encoding error): {filepath}")
            return False

    newcontent = content.replace(oldword, newword)

    if newcontent != content:
        with open(filepath, 'w', encoding='utf-8') as f:
            f.write(newcontent)
        return True
    return False

def process_directory(target_dir, oldword, newword, extensions):
    count = 0
    for root, dirs, files in os.walk(target_dir):
        for file in files:
            if any(file.endswith(ext) for ext in extensions):
                full_path = os.path.join(root, file)
                print(f"🔍 Processing: {full_path}")
                if replace_in_file(full_path, oldword, newword):
                    count += 1
    print(f"\n✅ Modified {count} file(s) with extensions {extensions} in '{target_dir}'.")

def main():
    if len(sys.argv) < 5:
        print("Usage: python find_replace.py <directory> <oldword> <newword> <extensions>")
        print("Example: python find_replace.py ./src/ 'foo' 'bar' .js,.json,.py")
        sys.exit(1)

    target_dir = sys.argv[1]
    oldword = sys.argv[2]
    newword = sys.argv[3]
    extensions = sys.argv[4].split(",")
    process_directory(target_dir, oldword, newword, extensions)

if __name__ == "__main__":
    main()
```


---

Let me know if you'd like to add backup creation, regex support, or a dry-run mode next!